/**
 * 
 */
package net.majorkernelpanic.streaming;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.text.format.DateFormat;
import android.util.Log;

/**
 * @author Vasanth
 * 
 */
public class buffer implements Runnable {

	private InputStream receiver;
	private OutputStream sender;
	private Thread t;

	public buffer(InputStream receiver, OutputStream sender) {
		this.receiver = receiver;
		this.sender = sender;
		Log.d("buffer", "Done");
	}

	public void start() throws IOException {
		if (t == null) {
			t = new Thread(this);
			t.start();
		}
	}

	public void stop() {
		try {
			receiver.close();
		} catch (IOException ignore) {
		}
		t.interrupt();
		t = null;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */

	@Override
	public void run() {
		// TODO Auto-generated method stub

		// ////////////////////////////////////////////////////////////////////
		// Copy From Socket to Buffer
		int bytesRead = 0;
		int current = 0;
		int filesize = 1400000;
		byte[] mybytearray = new byte[filesize];
		boolean condition = true;
		// Log.d("buffer file", "byte allocated");

		try {
			while (!Thread.interrupted()) {

				Calendar cal = Calendar.getInstance();
				long startTime = cal.getTimeInMillis();
				long currentTime;

				while (condition) {

					do {

						bytesRead = receiver.read(mybytearray, 0,
								mybytearray.length);
						
						// New Addition
						//bytesRead = receiver.read(mybytearray);
						// New Addition
						
						current = bytesRead;
						bytesRead = receiver.read(mybytearray, current,
								(mybytearray.length - current));
						// Log.d("receive","read"+bytesRead);
						// Log.d("receive","current"+current);
						current += bytesRead;

						// Copy from Buffer to Buffer to Socket
						sender.write(mybytearray, 0, current);
						// sender.write(mybytearray);
						sender.flush();
						// current = 0;
						// Log.d("sender","sent" +mybytearray);
						
						// New Addition
						/*mybytearray = new byte[0];
						current = 0;
						bytesRead = 0;*/
						// New Addition
						
						currentTime = Calendar.getInstance().getTimeInMillis();

					} while ((currentTime - startTime) <= 15 * 1000);
					condition = false;
					Log.d("Current Time", " Streamed Video of "
							+ (currentTime - startTime) + "seconds");
				}

				startTime = Calendar.getInstance().getTimeInMillis();

				bytesRead = 0;
				current = 0;

				// Log.d("Bytes Read", " Bytes Read "+bytesRead);
				// Log.d("Current", " Current "+current);

				do {

					bytesRead = receiver.read(mybytearray, 0,
					mybytearray.length);
			
					// New Addition
					//bytesRead = receiver.read(mybytearray);
					// New Addition
			
					current = bytesRead;
					bytesRead = receiver.read(mybytearray, current,
							(mybytearray.length - current));
					// Log.d("receive","read"+bytesRead);
					// Log.d("receive","current"+current);
					current += bytesRead;
					
					
					currentTime = Calendar.getInstance().getTimeInMillis();

				} while ((currentTime - startTime) <= 2 * 1000);

				// Copy from Buffer to Buffer to Socket
				//sender.write(mybytearray, 0, current);
				
				// New Addition
				
				sender.write(mybytearray);

				// New Addition

				sender.flush();
				
				// New Addition
				/*mybytearray = new byte[0];
				current = 0;
				bytesRead = 0;*/
				// New Addition
				
				Log.d("Current Time", " Buffered For "
						+ (currentTime - startTime) + "seconds");

			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// //////////////////////////////////////////////////////////////////

	}

}
